import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'controller.dart';

class StateGetBuilderView extends StatelessWidget {
  StateGetBuilderView({super.key});

  final controller = CountController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("getBuilder"),
      ),
      body: Center(
        child: Column(
          children: [
            GetBuilder<CountController>(
                init: controller,
                initState: (_) {},
                builder: (_) {
                  if (kDebugMode) {
                    print("GetBuilder - 1");
                  }
                  return Text('value -> ${_.count}');
                }),
            GetBuilder<CountController>(
                init: controller,
                initState: (_) {},
                builder: (_) {
                  if (kDebugMode) {
                    print("GetBuilder - 2");
                  }
                  return Text('value -> ${_.count}');
                }),
            const Divider(),
            GetBuilder<CountController>(
                init: controller,
                initState: (_) {},
                builder: (_) {
                  if (kDebugMode) {
                    print("GetBuilder - 3");
                  }
                  return Column(
                    children: [
                      Text("value - >${_.count}"),
                      ElevatedButton(
                          onPressed: () {
                            _.add();
                          },
                          child: const Text("GetBuilder -> add"))
                    ],
                  );
                }),
            const Divider(),

            // const2
            GetBuilder<CountController>(
              init: controller,
              initState: (_) {},
              builder: (_) {
                if (kDebugMode) {
                  print("GetBuilder - 4");
                }
                return Text('value count2 -> ${_.count2}');
              },
            ),
            const Divider(),

            // id2
            GetBuilder<CountController>(
              id: "id2",
              init: controller,
              initState: (_) {},
              builder: (_) {
                if (kDebugMode) {
                  print("GetBuilder - 4");
                }
                return Text('id2 -> value count2 -> ${_.count2}');
              },
            ),
            // 按钮
            ElevatedButton(
              onPressed: () {
                controller.add();
              },
              child: const Text('add'),
            ),

            ElevatedButton(
              onPressed: () {
                controller.add2();
              },
              child: const Text('add2'),
            ),

            ElevatedButton(
              onPressed: () {
                controller.update();
              },
              child: const Text('controller.update()'),
            ),

            ElevatedButton(
              onPressed: () {
                controller.update(["id2"]);
              },
              child: const Text('controller.update(id2)'),
            ),
          ],
        ),
      ),
    );
  }
}
